package com.fwlst.module_home

import android.os.Bundle
import androidx.fragment.app.Fragment
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.example.module_hp_zao_yin.HpZaoYinMainFragment
import com.example.module_hzd_host.FenBeiSecondPageFragment
import com.example.module_hzd_host.FenBeiThirdPageFragment
import com.fwlst.lib_base.activity.BaseMvvmActivity
import com.fwlst.lib_base.constant.GlobalUserInfo
import com.fwlst.lib_base.model.BaseViewModel
import com.fwlst.lib_base.utils.AdUtils.destroyBannerAd
import com.fwlst.lib_base.utils.AdUtils.loadBannerAd
import com.fwlst.lib_base.utils.AdUtils.loadNewInteractionAd
import com.fwlst.lib_base.utils.BaseUtils
import com.fwlst.module_home.databinding.ActivityHomeTabBinding
import com.fwlst.module_home.fragment.Tab1Fragment
import com.fwlst.module_home.fragment.TabSettingFragment
import com.fwlst.module_home.setting.MySettingFragment
import com.umeng.analytics.MobclickAgent

@Route(path = "/home/route/HomeTabActivity")
class HomeTabActivity : BaseMvvmActivity<ActivityHomeTabBinding, BaseViewModel>() {
    private val mFragments: MutableList<Fragment> = ArrayList()
    private var lastPosition = 0 //上次fragment的位置

    override fun initLayout(): Int {
        return R.layout.activity_home_tab
    }

    override fun initBR(): Int {
        return 0
    }

    override fun initView() {
        refreshUserInfo()
        initFragment()
    }

    private fun refreshUserInfo() {
        //更新用户信息
        GlobalUserInfo.updateUserInfo()
        loadNewInteractionAd(this)
        loadBannerAd(this, binding.bannerContainer)
    }

    private fun initFragment() {
        binding.bottomNavigationView.itemIconTintList = null
        binding.bottomNavigationView.itemIconSize = 190
        val mFragment1 = HpZaoYinMainFragment()
        val mFragment2 = FenBeiSecondPageFragment()
        val mFragment3 = FenBeiThirdPageFragment()
        val mFragment4 = MySettingFragment()
        mFragments.add(mFragment1)
        mFragments.add(mFragment2)
        mFragments.add(mFragment3)
        mFragments.add(mFragment4)
        if (mSavedInstanceState == null) {
            //根据传入的Bundle对象判断是正常启动还是重建 true表示正常启动，false表示重建
            setSelectedFragment(0)
        }
        binding.bottomNavigationView.setOnNavigationItemSelectedListener { menuItem ->
            val itemId: Int = menuItem.itemId
            if (itemId == R.id.tab_home) {
                setSelectedFragment(0)
            } else if (itemId == R.id.tab_2) {
                setSelectedFragment(1)
            }else if (itemId == R.id.tab_3) {
                setSelectedFragment(2)
            } else if (itemId == R.id.tab_setting) {
                setSelectedFragment(3)
            }
            true
        }
    }

    private fun setSelectedFragment(position: Int) {
        BaseUtils.setStatusBarLucency(this)
        val fragmentManager = supportFragmentManager
        val transaction = fragmentManager.beginTransaction()
        //要显示的Fragment
        var currentFragment =
            fragmentManager.findFragmentByTag("fragment$position") //要显示的fragment(解决了activity重建时新建实例的问题)
        //要隐藏的Fragment
        val hideFragment =
            fragmentManager.findFragmentByTag("fragment$lastPosition") //要隐藏的fragment(解决了activity重建时新建实例的问题)
        if (position != lastPosition) { //如果位置不同
            if (hideFragment != null) { //如果要隐藏的fragment存在，则隐藏
                transaction.hide(hideFragment)
            }
            if (currentFragment == null) { //如果要显示的fragment不存在，则新加并提交事务
                currentFragment = mFragments[position]
                transaction.add(R.id.fragment, currentFragment, "fragment$position")
            } else { //如果要显示的存在则直接显示
                transaction.show(currentFragment)
            }
        }

        if (position == lastPosition) { //如果位置相同
            if (currentFragment == null) { //如果fragment不存在(第一次启动应用的时候)
                currentFragment = mFragments[position]
                transaction.add(R.id.fragment, currentFragment, "fragment$position")
            } //如果位置相同，且fragment存在，则不作任何操作
        }
        transaction.commit() //提交事务
        lastPosition = position //更新要隐藏的fragment的位置
    }


    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        outState.putInt("last_position", lastPosition) //activity重建时保存页面的位置
    }

    override fun onRestoreInstanceState(savedInstanceState: Bundle) {
        super.onRestoreInstanceState(savedInstanceState)
        lastPosition = savedInstanceState.getInt("last_position") //获取重建时的fragment的位置
        setSelectedFragment(lastPosition) //恢复销毁前显示的fragment
    }

    override fun onResume() {
        super.onResume()
        MobclickAgent.onPageStart("HomeTabActivity")
    }

    override fun onPause() {
        super.onPause()
        MobclickAgent.onPageEnd("HomeTabActivity")
    }

    override fun onDestroy() {
        super.onDestroy()
        destroyBannerAd()
    }


}